require(['../config'],()=>{
    require(['bootstrap','jquery'],()=>{
        class Login{
            constructor(){
                this.addressActive=false
                this.testTel=/^1[3456789]\d{9}$/
                this.users=JSON.parse(localStorage.getItem('users')||'[]')
                this.chooseAddress()
                this.login()
            }
            chooseAddress(){
                const _this=this
                const $addressWarp=$('.addressWarp')
                const $address=$addressWarp.children('ul').children('li')
                const $choosed=$('.address .choose')
                $choosed.on('click',(e)=>{
                    $addressWarp.show()
                    this.addressActive=true
                    e.stopPropagation()
                })
                $address.on('click',function(e){
                    $choosed.html(e.target.innerText)
                    $addressWarp.hide()
                    _this.addressActive=false
                    $('.name span').html($(this).data('num'))
                })
                $(document).on('click',()=>{
                    if(this.addressActive){
                        $addressWarp.hide()
                    }
                })
            }
            loginSuccess(tel,pwd){
                this.users.push({name:tel,pwd})
                localStorage.setItem('users',JSON.stringify(this.users))
                document.cookie=`name=${tel};path=/`
                location.href='/'
            }
            login(){
                const $devlop=$('.devlop')
                const $noIntface=$('.noIntface')
                const $submit=$('button')
                const $title=$('.name .title')
                const $userInput=$('input[name=username]')
                const $password=$('input[name=password]')
                const $showPwd=$password.next()
                $showPwd.on('click',function(){
                    $(this).toggleClass('show')
                    if($password.attr('type')==='password'){
                        $password.attr('type','text')
                    }else{
                        $password.attr('type','password')
                    }
                })
                $noIntface.on('click',function(){
                    $devlop.show()
                    setTimeout(()=>{
                        $devlop.stop().hide({duration:500})
                    },500)
                })
                $userInput.on('input',function(){
                    $title.hide()
                })
                $submit.on('click',()=>{
                    const tel=$userInput.val()
                    const pwd=$password.val()
                    const isCan = this.testTel.test(tel)
                    if(isCan){
                        if(this.users.length>0){
                            const isRepeat=this.users.find(val=>{
                                return val.name===tel
                            })
                            if(!isRepeat){
                               this.loginSuccess(tel,pwd)
                            }else{
                                if(isRepeat.name===tel&&isRepeat.pwd===pwd){
                                    this.loginSuccess(tel,pwd)
                                }else{
                                    $devlop.html('*此用户名已存在').show()
                                    setTimeout(()=>{
                                        $devlop.stop().hide({duration:500},function(){
                                            $devlop.html('暂不支持该功能!!!')
                                        })
                                    },500)
                                }
                            }
                        }else{
                            this.loginSuccess(tel,pwd)
                        }
                    }else{
                        $title.show()
                    }
                })
            }
        }
        new Login()
    })
})